package cn.iocoder.yudao.module.project.service.constructionunit;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.project.controller.admin.constructionunit.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.constructionunit.ConstructionUnitDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.project.dal.mysql.constructionunit.ConstructionUnitMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.project.enums.ErrorCodeConstants.*;

/**
 * 施工单位 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ConstructionUnitServiceImpl implements ConstructionUnitService {

    @Resource
    private ConstructionUnitMapper constructionUnitMapper;

    @Override
    public Long createConstructionUnit(ConstructionUnitSaveReqVO createReqVO) {
        // 插入
        ConstructionUnitDO constructionUnit = BeanUtils.toBean(createReqVO, ConstructionUnitDO.class);
        constructionUnitMapper.insert(constructionUnit);

        // 返回
        return constructionUnit.getId();
    }

    @Override
    public void updateConstructionUnit(ConstructionUnitSaveReqVO updateReqVO) {
        // 校验存在
        validateConstructionUnitExists(updateReqVO.getId());
        // 更新
        ConstructionUnitDO updateObj = BeanUtils.toBean(updateReqVO, ConstructionUnitDO.class);
        constructionUnitMapper.updateById(updateObj);
    }

    @Override
    public void deleteConstructionUnit(Long id) {
        // 校验存在
        validateConstructionUnitExists(id);
        // 删除
        constructionUnitMapper.deleteById(id);
    }

    @Override
        public void deleteConstructionUnitListByIds(List<Long> ids) {
        // 删除
        constructionUnitMapper.deleteByIds(ids);
        }


    private void validateConstructionUnitExists(Long id) {
        if (constructionUnitMapper.selectById(id) == null) {
            throw exception(CONSTRUCTION_UNIT_NOT_EXISTS);
        }
    }

    @Override
    public ConstructionUnitDO getConstructionUnit(Long id) {
        return constructionUnitMapper.selectById(id);
    }

    @Override
    public PageResult<ConstructionUnitDO> getConstructionUnitPage(ConstructionUnitPageReqVO pageReqVO) {
        return constructionUnitMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ConstructionUnitDO> getConstructionUnitList(Collection<Long> ids, Collection<Integer> statuses) {
        return constructionUnitMapper.selectList(ids, statuses);
    }

}